# -*- coding: utf-8 -*-

import time

from PageClass.boe_DomesticTravelClass.Class_DomesticTravelBoe import Class_DomesticTravelBoe
from TestCases.common import LoginDepend
from TestCases.common._baseClass import _BaseClass
from Util import config, logger, record


class BoeSubmit(_BaseClass):

    def __init__(self, isExit=True):
        self.isExit = isExit
        self.login = LoginDepend('ecsHost', 'user')
        self.DomesticTravel = Class_DomesticTravelBoe(self.login.driver)

    def __exit__(self, *args):
        if super().__exit__(*args):
            if self.isExit:
                self.exit()
        else:
            self.exit()

    def exit(self):
        self.DomesticTravel.driver.quit()

    def submit(self, boe_data):
        # 打开费报
        logger.info(f"费报地址为：{config.urlDict.url.ecsHost}")  # .\Config\test\UrlConfig.yml

        # 打开差旅报账单
        self.DomesticTravel.open_boe(boe_data.areaType, boe_data.boeType, boe_data.operationType)

        # 获取单号
        boeNo = self.DomesticTravel.getBoeNum()
        record.writeDataToRecord({'boeNo': boeNo})

        # 主表区
        operationType = boe_data.operationType
        self.DomesticTravel.input_operationType(operationType)

        # 发票区
        # 选择其他纸质票新增
        for invoiceData in boe_data.invoiceData:
            logger.info(f"发票类型：{invoiceData.invoiceType}，发票参数：{invoiceData}")
            self.DomesticTravel.newInvoice(invoiceData)

        # 为发票选择费用分类
        time.sleep(1)
        # 点击
        self.DomesticTravel.FeeNameClick()
        # 弹框搜索、选择  # mini弹框
        self.DomesticTravel.selectValueFromMiniTable(**{'业务小类编码': boe_data.feeCode, '业务小类名称': boe_data.feeName})

        # 费用归属区
        Project = boe_data.Project
        self.DomesticTravel.inputProject(Project)

        # 提交单据
        message = self.DomesticTravel.click_boeSubmitButton()

        return message
